package leeon.bbsbrower.actions;

import ipworks.IPWorksException;

import java.util.HashMap;
import java.util.Map;
import java.util.TooManyListenersException;

import leeon.bbsbrower.models.FootObject;
import leeon.bbsbrower.util.HTMLUtil;
import leeon.bbsbrower.util.HTTPBean;
import leeon.bbsbrower.views.ApplicationActionBarAdvisor;
import leeon.bbsbrower.views.DocView;

public class BBSLogAction {
	
	public static final String URL_LOGIN = Config.SERVER_URL + Config.SERVER_CGI + "bbslogin";
	
	public static final String URL_FOOT = Config.SERVER_URL + Config.SERVER_CGI + "bbsfoot";
	
	public static final String URL_LOGOUT = Config.SERVER_URL + Config.SERVER_CGI + "bbslogout";
	
	public static String id = null;
	
	//0 一般，1 主题
	public static int t = 0;
	
	/**
	 * 登录动作
	 * @param id
	 * @param pw
	 * @return 成功返回true
	 * @throws TooManyListenersException 
	 * @throws IPWorksException 
	 * @throws ContentException 
	 */
	public static void login(String id, String pw) throws IPWorksException, ContentException {		

		HTTPBean http = HTTPBean.newInstance(Config.PROXY_SERVER, Config.PROXY_PORT, Config.PROXY_USER, Config.PROXY_PASSWORD);			
			
		if (id != null && id.length() != 0) {
			String content = http.get(URL_LOGIN +"?id="+id+"&pw="+pw);
			if (content.indexOf("错误") == -1) {
				Map<String, String> map = findLoginCookie(content);
				http.addCookie(map);
				BBSLogAction.id = id;
				
				FootObject f = foot();
				ApplicationActionBarAdvisor.setStatusLine(f.toString());
			} else {
				throw new ContentException(content);
			}
		} else {
			http.addCookie(setDefaultCookie(true));
			BBSLogAction.id = "guest";
		}
	}
	
	
	/**
	 * 默认的cookie设置
	 * @return
	 */
	private static Map<String, String> setDefaultCookie(boolean add) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("my_t_lines", add?String.valueOf(DocView.size):"");		
		return map;
	}
	
	/**
	 * 查找登陆时需要在cookie里放的东西
	 * @param content
	 * @return
	 */
	private static Map<String, String> findLoginCookie(String content) {
		Map<String, String> map = new HashMap<String, String>();
		String value = HTMLUtil.findStr(content, "utmpnum", "'", 1);
		if (value != null) map.put("utmpnum", value);
		
		value = HTMLUtil.findStr(content, "utmpkey", "'", 1);
		if (value != null) map.put("utmpkey", value);
		
		value = HTMLUtil.findStr(content, "utmpuserid", "'", 1);
		if (value != null) map.put("utmpuserid", value);
		
		value = HTMLUtil.findStr(content, "my_t_lines", "'", 1);
		if (value != null) {
			map.put("my_t_lines", value);
			if (value.length() != 0)
				DocView.size = Integer.parseInt(value);
		} else 
			map.put("my_t_lines", String.valueOf(DocView.size));
		
		value = HTMLUtil.findStr(content, "my_link_mode", "'", 1);
		if (value != null) map.put("my_link_mode", value);
		
		value = HTMLUtil.findStr(content, "my_def_mode", "'", 1);
		t = "1".equals(value)?1:0;
		if (value != null) map.put("my_def_mode", value);
		
		value = HTMLUtil.findStr(content, "my_style", "'", 1);
		if (value != null) map.put("my_style", value);
		
		return map;
	}
	
	public static FootObject foot() throws IPWorksException {
		HTTPBean http = HTTPBean.newInstance(Config.PROXY_SERVER, Config.PROXY_PORT, Config.PROXY_USER, Config.PROXY_PASSWORD);
		String content = http.get(URL_FOOT);
		String id = "guest";
		String mailNumber = null;
			
			//非guest登陆
		if (content.indexOf("用户") == -1) {
			id =  HTMLUtil.findStr(content, "<b>", "</b>");
			mailNumber = HTMLUtil.findStr(content, "<a href=bbsmail target=view>", "</a>");
			mailNumber = mailNumber.replaceAll("</font>", "").replaceAll("<font color=red>", "");
		}
			
		String onlineNumber = HTMLUtil.findStr(content, "<a href=bbsusr target=view>", "</a>");
		String loginTime = HTMLUtil.findStr(content, "<img border=0 src=/images/water.gif align=absmiddle>", "</td>");
			
		return new FootObject(id, onlineNumber, mailNumber, loginTime);
	}
	
	public static void logout() throws IPWorksException {
		HTTPBean http = HTTPBean.newInstance(Config.PROXY_SERVER, Config.PROXY_PORT, Config.PROXY_USER, Config.PROXY_PASSWORD);
		if (!"guest".equals(BBSLogAction.id)) {							
			String content = http.get(URL_LOGOUT);
			Map<String, String> map = findLoginCookie(content);
			http.addCookie(map);
		} else {
			http.addCookie(setDefaultCookie(false));				
		}
		BBSLogAction.id = null;
	}
	
	/**
	 * @param args
	 * @throws InterruptedException 
	 * @throws ContentException 
	 * @throws IPWorksException 
	 */
	public static void main(String[] args) throws InterruptedException, IPWorksException, ContentException {
	}

}
